<template>
  <view class="page_diy_view page_dormitory_information_view" :style="{ paddingTop: vuex_custom_bar_height + 'px' }">
    <tn-nav-bar>宿舍信息详情</tn-nav-bar>
    <view id="page_diy_view" class="page-from">
      <uni-forms :modelValue="form" v-if="is_view()" labelWidth="70px">

        <uni-forms-item v-if="$check_field('get','dormitory_name') || ($check_field('add','dormitory_name') || $check_field('set','dormitory_name'))" label="宿舍名称" name="dormitory_name">
                      <uni-easyinput type="text" v-model="form['dormitory_name']" v-if="(form['dormitory_information_id'] && $check_field('set','dormitory_name')) || (!form['dormitory_information_id'] && $check_field('add','dormitory_name'))" :disabled="disabledObj['dormitory_name_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','dormitory_name')">
            {{ form['dormitory_name'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','name_of_dormitory_manager') || ($check_field('add','name_of_dormitory_manager') || $check_field('set','name_of_dormitory_manager'))" label="宿管姓名" name="name_of_dormitory_manager">
                      <uni-easyinput type="text" v-model="form['name_of_dormitory_manager']" v-if="(form['dormitory_information_id'] && $check_field('set','name_of_dormitory_manager')) || (!form['dormitory_information_id'] && $check_field('add','name_of_dormitory_manager'))" :disabled="disabledObj['name_of_dormitory_manager_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','name_of_dormitory_manager')">
            {{ form['name_of_dormitory_manager'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','dormitory_location') || ($check_field('add','dormitory_location') || $check_field('set','dormitory_location'))" label="宿舍位置" name="dormitory_location">
                      <uni-easyinput type="text" v-model="form['dormitory_location']" v-if="(form['dormitory_information_id'] && $check_field('set','dormitory_location')) || (!form['dormitory_information_id'] && $check_field('add','dormitory_location'))" :disabled="disabledObj['dormitory_location_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','dormitory_location')">
            {{ form['dormitory_location'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','cover_photo') || ($check_field('add','cover_photo') || $check_field('set','cover_photo'))" label="封面图片" name="cover_photo">
              <!-- 修改权限 -->
          <view class="from-img" v-if="form['cover_photo'] && $check_field('set', 'cover_photo')">
            <image v-if="disabledObj['cover_photo_isDisabled']" :src="$fullImgUrl(form['cover_photo'])" />
            <image v-if="!disabledObj['cover_photo_isDisabled']" :src="$fullImgUrl(form['cover_photo'])" @click="change_img('cover_photo')" />
          </view>
          <!-- 添加权限 -->
          <view class="from-img" v-else-if="!form['cover_photo'] && ($check_field('add','cover_photo') || $check_field('set','cover_photo'))">
            <view v-if="disabledObj['cover_photo_isDisabled']" class="add-img">
              <text>+</text>
            </view>
            <view v-if="!disabledObj['cover_photo_isDisabled']" class="add-img" @click="change_img('cover_photo')">
              <text>+</text>
            </view>
          </view>
          <!-- 查询权限 -->
          <view class="from-img" v-else-if="$check_field('get', 'cover_photo')">
            <image :src="$fullImgUrl(form['cover_photo'])" />
          </view>
            </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','dormitory_building') || ($check_field('add','dormitory_building') || $check_field('set','dormitory_building'))" label="宿舍楼栋" name="dormitory_building">
                      <uni-easyinput type="text" v-model="form['dormitory_building']" v-if="(form['dormitory_information_id'] && $check_field('set','dormitory_building')) || (!form['dormitory_information_id'] && $check_field('add','dormitory_building'))" :disabled="disabledObj['dormitory_building_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','dormitory_building')">
            {{ form['dormitory_building'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','number_of_residents') || ($check_field('add','number_of_residents') || $check_field('set','number_of_residents'))" label="居住人数" name="number_of_residents">
                      <uni-easyinput type="text" v-model="form['number_of_residents']" v-if="(form['dormitory_information_id'] && $check_field('set','number_of_residents')) || (!form['dormitory_information_id'] && $check_field('add','number_of_residents'))" :disabled="disabledObj['number_of_residents_isDisabled']" />
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get','number_of_residents')">
            {{ form['number_of_residents'] }}
          </text>
                    </uni-forms-item>
        <uni-forms-item v-if="$check_field('get','dormitory_details') || ($check_field('add','dormitory_details') || $check_field('set','dormitory_details'))" label="宿舍详情" name="dormitory_details">
              <uni-easyinput type="textarea" v-model="form['dormitory_details']" v-if="(form['dormitory_information_id'] && $check_field('set', 'dormitory_details')) || (!form['dormitory_information_id'] && $check_field('add', 'dormitory_details'))" :disabled="disabledObj['dormitory_details_isDisabled']"/>
          <!-- 仅查看 -->
          <text v-else-if="$check_field('get', 'dormitory_details')">
            {{ form['dormitory_details'] }}
          </text>
            </uni-forms-item>
      </uni-forms>
      <view class="form-footer" v-if="$check_action('/dormitory_information/view','set') || ($check_action('/dormitory_information/view','add') || $check_option('/dormitory_information/table','examine'))">
        <view class="me-btn btn-submit" @click="submit_()"> 提交 </view>
        <view class="me-btn btn-cancel" @click="cancel()">取消</view>
      </view>
      <view class="form_button" v-else>
        <view class="me-btn btn-cancel" @click="cancel()">返回</view>
      </view>
    </view>
  </view>
</template>

<script>
import mixin from '@/libs/mixins/page.js';
                            import { uploadFilePathApi, uploadFileApi } from '@/api/common.js';
export default {
  mixins: [mixin],
  data() {
    return {
	  today: new Date().toISOString().split('T')[0],
      field: "dormitory_information_id",
      url_add: "~/api/dormitory_information/add?",
      url_set: "~/api/dormitory_information/set?",
      url_get_obj: "~/api/dormitory_information/get_obj?",
      url_upload: "~/api/dormitory_information/upload?",
      dormitory_name: null,
      name_of_dormitory_manager: null,
      dormitory_location: null,
      cover_photo: null,
      dormitory_building: null,
      number_of_residents: null,
      dormitory_details: null,
      query: {
        "dormitory_information_id": 0,
      },
      form: {
          "dormitory_name":  '', // 宿舍名称
            "name_of_dormitory_manager":  '', // 宿管姓名
            "dormitory_location":  '', // 宿舍位置
            "cover_photo":  '', // 封面图片
            "dormitory_building":  '', // 宿舍楼栋
            "number_of_residents":  0 , // 居住人数
            "dormitory_details":  '', // 宿舍详情
          "dormitory_information_id": 0, // ID
      },
      disabledObj:{
          "dormitory_name_isDisabled": false,
            "name_of_dormitory_manager_isDisabled": false,
            "dormitory_location_isDisabled": false,
            "cover_photo_isDisabled": false,
            "dormitory_building_isDisabled": false,
                "dormitory_details_isDisabled": false,
        },
                                                            };
  },
  methods: {
    /**
     * 提交前验证事件
     * @param {Object} 请求参数
     * @return {String} 验证成功返回null, 失败返回错误提示
     */
    async submit_check(param) {
                                                                                                                      return null;
    },
    changeLog(v, value) {
      this.form[value] = v;
    },
    uploadFile_(type) {
      // #ifdef APP-VUE
      uni.chooseFile({
        count: 1,
        success: (chooseImageRes) => {
          const tempFilePaths = chooseImageRes.tempFilePaths;
          this.successChoose(tempFilePaths[0], type);
        },
      });
      // #endif
      // #ifdef !APP-VUE
      if (
        uni.getSystemInfoSync().uniPlatform != 'mp-weixin' ||
        uni.getSystemInfoSync().platform == 'devtools'
      ) {
        uni.chooseImage({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            this.successChoose(tempFilePaths[0], type);
          },
        });
      } else {
        wx.chooseMessageFile({
          count: 1,
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFiles;
            this.successChoose(tempFilePaths[0].path, type);
          },
        });
      }
      // #endif
    },
    successChoose(filePath, type) {
      uni.uploadFile({
        url: this.$fullUrl('~/api/dormitory_information/upload?'), //仅为示例，非真实的接口地址
        filePath,
        name: 'file',
        success: (uploadFileRes) => {
          if (uploadFileRes.data.error) {
            uni.showToast({ title: uploadFileRes.data.error.message, icon: 'none' });
          } else {
            this[type] = JSON.parse(uploadFileRes.data).result.url;
          }
        },
      });
    },
    close_(type) {
      if (type == 'dormitory_name') this['dormitory_name'] = this.form['dormitory_name'] = "";
      if (type == 'name_of_dormitory_manager') this['name_of_dormitory_manager'] = this.form['name_of_dormitory_manager'] = "";
      if (type == 'dormitory_location') this['dormitory_location'] = this.form['dormitory_location'] = "";
      if (type == 'cover_photo') this['cover_photo'] = this.form['cover_photo'] = "";
      if (type == 'dormitory_building') this['dormitory_building'] = this.form['dormitory_building'] = "";
      if (type == 'number_of_residents') this['number_of_residents'] = this.form['number_of_residents'] = "";
      if (type == 'dormitory_details') this['dormitory_details'] = this.form['dormitory_details'] = "";
    },
    submit_() {
      if (this['dormitory_name'] !== null) this.form['dormitory_name'] = this['dormitory_name']
      if (this['name_of_dormitory_manager'] !== null) this.form['name_of_dormitory_manager'] = this['name_of_dormitory_manager']
      if (this['dormitory_location'] !== null) this.form['dormitory_location'] = this['dormitory_location']
      if (this['cover_photo'] !== null) this.form['cover_photo'] = this['cover_photo']
      if (this['dormitory_building'] !== null) this.form['dormitory_building'] = this['dormitory_building']
      if (this['number_of_residents'] !== null) this.form['number_of_residents'] = this['number_of_residents']
      if (this['dormitory_details'] !== null) this.form['dormitory_details'] = this['dormitory_details']
      console.log(this.form)
      this.submit();
    },
    /**
     * 上传文件
     * @param {Object} param文件参数
     */
    change_file(key_name) {
      var _self = this;
      // 选择图像方法
      uni.chooseFile({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi(
            '/dormitory_information/upload',
            filePath,
            undefined,
            {
              i_want_to_customize: 'test',
            },
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 上传图片
     * @param {Object} param文件参数
     */
    change_img(key_name) {
      var _self = this;
      _self.upload_img_flag = false;
      // 选择图像方法
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (file) => {
          const tempFilePaths = file.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi('/dormitory_information/upload',filePath,undefined,undefined,
            (task) => {
              task.onProgressUpdate((res) => {
                this.percent = res.progress;
                console.log('上传进度' + res.progress);
                console.log('已经上传的数据长度' + res.totalBytesSent);
                console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
              });
            }
          ).then((res) => {
            const filename = res.result.url;
            this.form[key_name] = filename;
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    /**
     * 获取对象后获取缓存表单
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_before(param) {
      var form = uni.db.get('form');
      if (form) {
        delete form.examine_state;
        delete form.examine_reply;
        this.obj = uni.push(this.obj, form);
        this.form = uni.push(this.form, form);
      }
      var arr = [];
      for (let key in form) {
        arr.push(key);
      }
      for (var i = 0; i < arr.length; i++) {
        this.disabledObj[arr[i] + '_isDisabled'] = true;
      }
                                  uni.db.del('form');

      return param;
    },
    
      
      
      
      
      
      
      /**
     * 获取对象之后
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func) {
                                                                                        },

    is_view() {
      var bl = this.user_group == '管理员';

      if (!bl) {
        bl = this.$check_action('/dormitory_information/table', 'add');
        console.log(bl ? '你有表格添加权限视作有添加权限' : '你没有表格添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/dormitory_information/table', 'set');
        console.log(bl ? '你有表格添加权限视作有修改权限' : '你没有表格修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/dormitory_information/view', 'add');
        console.log(bl ? '你有视图添加权限视作有添加权限' : '你没有视图添加权限');
      }
      if (!bl) {
        bl = this.$check_action('/dormitory_information/view', 'set');
        console.log(bl ? '你有视图修改权限视作有修改权限' : '你没有视图修改权限');
      }
      if (!bl) {
        bl = this.$check_action('/dormitory_information/view', 'get');
        console.log(bl ? '你有视图查询权限视作有查询权限' : '你没有视图查询权限');
      }

      console.log(
        bl
          ? '具有当前页面的查看权，请注意这不代表你有字段的查看权'
          : '无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行'
      );

      return bl;
    },
  },
  created() {
                              },
};
</script>

<style lang="scss" scoped>
  @import '@/styles/pagesC/index.scss';
</style>